<style>
.cib {
    border:solid 3px green;
}
</style>
<script>
function checkimage(obj) {
    if (obj.value == '')
    {
        return false;
    }
    var allowExt = '<?php echo $this->allow_ext; ?>';
    var fileExt = obj.value.substr(obj.value.lastIndexOf('.')+1).toLowerCase();
    if (allowExt.indexOf(fileExt)==-1)
    {
        alert('对不起，该类型的文件不允许上传，请上传图片文件('+allowExt+')');
        obj.value = '';
        return false;
    }

    return true;
}

// 初始化kindeditor
editor = '';
var eoptions = {
    width:'443px',
    height:'250px',

    uploadJson:'?app=product&mod=add&type=image&inajax=1&r='+Math.random(),
    fileManagerJson:'?app=portal&mod=filemanager',
    allowFileManager:true,
    allowMediaUpload:false,
    allowFlashUpload:false,
    urlType:'relative',
    items:['source','fullscreen','quickformat','preview','|','cut','copy','paste','wordpaste','|','fontname','fontsize','forecolor', 'hilitecolor', 'bold', 'italic', 'underline','|','selectall','justifyleft','justifycenter','justifyright','justifyfull','insertorderedlist','insertunorderedlist','|','indent','outdent','subscript','superscript','formatblock','|','image','flash','|','table','link','unlink']
};

KindEditor.ready(function(K){
    editor = K.create('#desc',eoptions);
});

// 商品图片数组
imageshash = new Array();

$(function(){
    // 选中系统类型
    $('#systemtype').val('<?php echo $this->product['systemtype']; ?>');
	cselectorInit('product_class');

    $('a').live('click',function(){
        var current_object = $(this);
        var action = current_object.attr('class');
        var hash = current_object.attr('rel');

        switch (action)
        {
            case 'setdefault':
                $('img').removeClass('cib');
                $('a[class=canceldefault]').html('设为默认');
                $('a[class=canceldefault]').attr('class','setdefault');
                $('#img'+hash).addClass('cib');
                $('#default_imagehash').val(hash);
                current_object.attr('class','canceldefault');
                current_object.html('取消默认');
                $('#default_imagehash').val(hash);
            break;

            case 'canceldefault':
                $('#default_imagehash').val('');
                $('img').removeClass('cib');
                current_object.attr('class','setdefault');
                current_object.html('设为默认');
            break;

            case 'delete':
                var dobj = $('#default_imagehash');
                if (dobj.val() == hash)
                {
                    dobj.val('');
                }
                $.post(location.href,{type:'deleteimage',hash:hash,r:Math.random()},function(){
                    $('#list'+hash).remove();
                    imageshash.removeByIndex(imageshash.getIndexByVal(hash));
                });
            break;

            case 'use':
                current_object.attr('class','setdefault');
                current_object.html('设为默认');
                $('#list'+hash).appendTo('#product_images_1');
            break;
        
        }
    });
    
    // 图片默认状态
    $('a[class=setdefault][rel=<?php echo $this->product['default_imagehash']; ?>]').click();

    $('#product_image_upload').click(function(){
        upload_button = $(this);
        upload_button.val('正在上传');
        upload_button.attr('disabled','true');
        $.ajaxFileUpload({
            'url' : '?app=product&mod=add&type=product_image&inajax=1&r='+Math.random(),
            secureuri : false,
            fileElementId : 'pi',
            dataType : 'json',
            success : function(data) {
                upload_button.removeAttr('disabled');
                upload_button.val('上传');
                if (data.state <= 0)
                {
                    alert(data.message);
                    return false;
                }

                imageshash.push(data.message);

                $('<div class="list" id="list'+data.message+'"><img id="img'+data.message+'" src="'+data.extra+'" rel="'+data.message+'" alt="查看原图" title="查看原图"><br><span class="fl"><a href="javascript:;" rel="'+data.message+'" id="img'+data.message+'" class="setdefault">设为默认</a></span><span class="fr"><a href="javascript:;" rel="'+data.message+'" class="delete">删除</a></span></div>').appendTo('#product_images_1');
            },
            error:function(x,s){
                alert('网络异常'+s);
                upload_button.removeAttr('disabled');
                upload_button.val('上传');
            },
            timeout:function(){
                alert('等待服务器响应时间过长，上传失败。');
                upload_button.removeAttr('disabled');
                upload_button.val('上传');
            }
        });
    });

    // tab切换
    $('.bg2 > span').click(function(){
        var rel = $(this).attr('rel');
        $('.bg2 > span').removeClass('span_now');
        $('.bg2 > span[rel='+rel+']').addClass('span_now');
        $('div[label]').css('display','none');
        $('div[label='+rel+']').css('display','block');
    });

    $('#form').submit(function(){
        // 合并图片
        $('#imagehashs').val(imageshash.join(','));
        // 同步编辑器内容
        editor.sync();
        // ajax发送数据
        $.post(location.href,$(this).serialize(),function(data){
            if (data.state <= 0)
            {
                if(data.extra != '') $('#'+data.extra).focus();
                alert(data.message);
                return false;
            }
			 alert(data.message);
            //dialogMessage(data.message,'green');
        },'json');
        return false;
    });
});
</script>

<div class="edit_tab tab">
<div class="bg1">
<div class="bg2">
      <span rel="info" class="span_now">商品信息</span>
      <span rel="product_image">商品图片</span>
</div>
</div>
</div>

<div label="info" class="edit_div">
<div class="line_1"></div>
<form id="form">
    <div class="form_div1">
        <span class="span1">商品名称</span>
        <span><input name="name" id="name" value="<?php echo $this->product['name']; ?>" /><font color="#848484">100个字符以内</font></span>
    </div>
    <div class="form_div1">
        <span class="span1">系统类型</span>
        <span>
            <select id="systemtype" name="systemtype">
                <?php foreach((array)explode("\n",$this->systemtype) as $type): ?>
                <option value="<?php echo $type; ?>"><?php echo $type; ?></option>
                <?php endforeach; ?>
            </select>
        </span>
    </div>
	<div class="form_div1">
        <span class="span1">库存分类</span>
        <span id="product_class">
			<strong><?php foreach((array)$this->class_names as $class): ?><?=$class['class_name']?>&nbsp;>&nbsp;<?php endforeach; ?></strong>
            <select style="width:auto;display:none">
				<option value="0">请选择</option>
                <?php foreach((array)$this->product_class as $item): ?>
                <option value="<?=$item['class_id']?>"><?=$item['class_name']?></option>
                <?php endforeach; ?>
            </select>
			<input type="button" rel="edit" class="cselector" value="修改" />
        </span>
    </div>
    <div class="form_div1">
        <span class="span1">货号</span>
        <span><input name="code" id="code" value="<?php echo $this->product['code']; ?>" /><font color="#848484">200个字符以内</font></span>
    </div>
    <div class="form_div1">
        <span class="span1">库存数量</span>
        <span><input name="num" id="num" value="<?php echo $this->product['num']; ?>" /></span>
    </div>
    <div class="form_div1">
        <span class="span1">成本单价</span>
        <span><input name="price" id="price" value="" /><font color="#848484">格式：00.00/00/00.0，小数点最多两位</font></span>
    </div>
    <div class="form_div1">
        <span class="span1">销售单价</span>
        <span><input name="sale_price" id="sale_price" value="" /><font color="#848484">格式：00.00/00/00.0，小数点最多两位</font></span>
    </div>
    <div class="line_1"></div>
    <div class="form_div1">
        <span class="span1">详情</span>
        <span><textarea id="desc" name="desc"><?php echo $this->product['desc']; ?></textarea></span>
    </div>
    <div class="line_1"></div>
    <div class="form_div1">
        <span class="span1"></span>
        <span><input name="" type="submit" class="btn1" value="确定" /><input name="" type="reset" class="btn1" value="重置" /><?php echo showhide(array('default_imagehash'=>'','imagehashs'=>'','type'=>'modi','object_id'=>'none','cselector_type'=>'product')); ?></span>
    </div>
    <div class="line_1"></div>
</form>
</div>

<div label="product_image" class="edit_div" style="display:none">
<div class="line_1"></div>
    <div class="form_div1">
        <span class="span1">商品图片</span>
        <span>
            <input type="file" id="pi" name="pi" onchange="checkimage(this)" rel="product_image" /><input type="button" id="product_image_upload" rel="product_image" value="上传"/><font color="#848484">请控制图片大小在1M以内，否则将上传失败</font>
        </span>
    </div>
    <div class="form_div_img" id="product_images_1">
    <?php foreach((array)$this->images as $image): ?>
        <div class="list" id="list<?php echo $image['hash']; ?>"><img id="img<?php echo $image['hash']; ?>" src="./attach/<?php echo $image['filepath'].'/'.$image['hash'].'.'.$image['ext']; ?>" rel="<?php echo $image['hash']; ?>" alt="查看原图" title="查看原图"><br><span class="fl"><a href="javascript:;" rel="<?php echo $image['hash']; ?>" id="img<?php echo $image['hash']; ?>" class="setdefault">设为默认</a></span><span class="fr"><a href="javascript:;" rel="<?php echo $image['hash']; ?>" class="delete">删除</a></span></div>
    <?php endforeach; ?>
    </div>
    <div class="line_1"></div>
</div>